package com.power.using.mytoutiao.module.base;

import android.os.Bundle;

import androidx.annotation.Nullable;

/**
 * Created by 聪明一只哈 on 2020/4/3.
 * Time: 2020/4/3  9:35
 */
public abstract class LazyLoadFragment <T extends IBasePresenter> extends
        BaseFragment<T>{


    protected boolean isViewInitiated;
    protected boolean isViibaleToUser;
    protected boolean isDataInitiated;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        preparedFetchData();

    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isViibaleToUser=isVisibleToUser;
        if(isVisibleToUser){
            preparedFetchData();
        }
    }

    public void preparedFetchData() {
        preparedFetchData(false);
    }

    public void preparedFetchData(boolean fourceUpdate) {
        if(isViibaleToUser&&isViewInitiated&&(!isDataInitiated||fourceUpdate)){
            fetchData();
            isDataInitiated=true;
        }

    }

    protected abstract void fetchData();


}
